package leetcode_1_200;

import com.sun.javafx.text.TextRun;

import java.util.HashSet;
import java.util.Set;

public class LeeCode_97 {
    public static void main(String[] args) {
//        System.out.println(isInterleave("aabcc", "dbbca", "aadbbcbcac"));
//        System.out.println(isInterleave("aabcc", "dbbca", "aadbbbaccc"));
//        System.out.println(isInterleave("bcc", "dbbca", "dbbbaccc"));
//        System.out.println(isInterleave("", "", ""));
//        System.out.println(isInterleave("aabcc", "dbbca", "aadbcbbcac"));
        System.out.println(isInterleave3("aabd", "abdc", "aabdabcd"));
        System.out.println(isInterleave("aabd", "abdc", "aabdabcd"));
        //System.out.println(isInterleave("", "", "a"));
    }

    private static boolean isInterleave(String s1, String s2, String s3) {
        int m = s1.length();
        int n = s2.length();
        if (s3.length() != m + n) {
            return false;
        }
        boolean[][] dp = new boolean[m + 1][n + 1];
        dp[0][0] = true;
        for (int i = 1; i <= m && s1.charAt(i - 1) == s3.charAt(i - 1); i++) {
            dp[i][0] = true;
        }
        for (int j = 1; j <= n && s2.charAt(j - 1) == s3.charAt(j - 1); j++) {
            dp[0][j] = true;
        }
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                dp[i][j] = (dp[i - 1][j] && s1.charAt(i - 1) == s3.charAt(i + j - 1)) ||
                        (dp[i][j - 1] && s2.charAt(j - 1) == s3.charAt(i + j - 1));
            }
        }
        return dp[m][n];
    }

    private static boolean isInterleave3(String s1, String s2, String s3) {
        int m = s1.length(), n = s2.length();
        if (s3.length() != m + n) return false;
        // 动态规划，dp[i,j]表示s1前i字符能与s2前j字符组成s3前i+j个字符；
        boolean[][] dp = new boolean[m + 1][n + 1];
        dp[0][0] = true;
        for (int i = 1; i <= m && s1.charAt(i - 1) == s3.charAt(i - 1); i++) dp[i][0] = true; // 不相符直接终止
        for (int j = 1; j <= n && s2.charAt(j - 1) == s3.charAt(j - 1); j++) dp[0][j] = true; // 不相符直接终止
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                dp[i][j] = (dp[i - 1][j] && s3.charAt(i + j - 1) == s1.charAt(i - 1))
                        || (dp[i][j - 1] && s3.charAt(i + j - 1) == s2.charAt(j - 1));
            }
        }
        return dp[m][n];
    }


    private static boolean isInterleave2(String s1, String s2, String s3) {
        boolean[] res = new boolean[1];
        Set<String> used = new HashSet<>();
        search(s1, s2, s3, res, used);
        return res[0];
    }

    private static void search(String s1, String s2, String s3, boolean[] res, Set<String> used) {
        if (used.contains(s1 + "," + s2))
            return;
        int len1 = s1.length();
        if (len1 == 0 && s2.equals(s3)) {
            res[0] = true;
            return;
        }
        int len2 = s2.length();
        if (len2 == 0 && s1.equals(s3)) {
            res[0] = true;
            return;
        }
        int len3 = s3.length();
        if (len1 + len2 != len3) {
            return;
        }
        int s1s = 0;
        for (int i = 0; i < len1; i++) {
            if (s1.charAt(i) == s3.charAt(i)) {
                s1s++;
                String s1_1 = s1.substring(s1s, len1);
                search(s1_1, s2, s3.substring(s1s, len3), res, used);
                used.add(s1_1 + "," + s2);
            } else
                break;
        }
        int s2s = 0;
        for (int i = 0; i < len2; i++) {
            if (s2.charAt(i) == s3.charAt(i)) {
                s2s++;
                String s2_2 = s2.substring(s2s, len2);
                search(s1, s2_2, s3.substring(s2s, len3), res, used);
                used.add(s1 + "," + s2_2);
            } else
                break;
        }
    }
}
